﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace hpcpdi
{
    public class MyExction : IAsyncExceptionFilter
    {
        /// <summary>
        /// NLog记录日志
        /// </summary>
        private ILogger<MyExction> _logger { get; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public MyExction(ILogger<MyExction> logger)
        {
            this._logger = logger;
        }
        /// <summary>
        /// 全局异常处理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Task OnExceptionAsync(ExceptionContext context)
        {
            //throw new System.NotImplementedException();
            if (!context.ExceptionHandled)
            {
                context.Result = new ContentResult
                {
                    Content = "服务器繁忙，请稍候重试"
                };
                _logger.LogError(context.Exception.Message);
            }
            return Task.CompletedTask;
        }
    }
}
